#!/bin/bash
cd /var2/mysql
month="$(date +%Y%m)"
schools=`ls`
echo $schools
for school in $schools;do
    test ! -d /var2/mysql/$school/db_bak && continue
    echo $school
    time="$(date +%Y-%m-%d_%k:%M:%S)"
    #dir & files
    logpath=/var/nas/$school/db_bak
    test ! -d $logpath && mkdir -p $logpath
    logfile=/var/nas/$school/db_bak/backup-$month.log
    test ! -f $logfile && touch $logfile

    echo "$time begin to zip dbs" >> $logfile
    fileName=`ls /var2/mysql/$school/db_bak`
    echo "fileName is $fileName" >> $logfile
    echo "dbs: `ls $school/db_bak/$fileName`" >> $logfile
    zip -qr /var/nas/$school/db_bak/$fileName.zip /var2/mysql/$school/db_bak/$fileName

    #validate
    sleep 1
    if [ -f "/var/nas/$school/db_bak/$fileName.zip" ]; then
        #statements
        echo "success! `ls -alh /var/nas/$school/db_bak/$fileName.zip`" >> $logfile
        rm -rf /var2/mysql/$school/db_bak
    else
        echo "error! $fileName not found." >> $logfile
    fi
done
